/*
 *
 *  *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 *  *  <p>
 *  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  *  you may not use this file except in compliance with the License.
 *  *  You may obtain a copy of the License at
 *  *  <p>
 *  * https://www.gnu.org/licenses/lgpl.html
 *  *  <p>
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */
package com.base.pig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.pig.constant.CacheConstants;
import com.base.pig.constant.enums.DictTypeEnum;
import com.base.pig.entity.SysDict;
import com.base.pig.entity.SysDictItem;
import com.base.pig.util.R;
import com.base.pig.service.SysDictService;

import com.base.pig.mapper.SysDictItemMapper;
import com.base.pig.service.SysDictItemService;

import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 字典项
 *
 * @author lengleng
 * @date 2019/03/19
 */
@Service
@AllArgsConstructor
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements SysDictItemService {

	private final SysDictService dictService;

	/**
	 * 删除字典项
	 *
	 * @param id 字典项ID
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R removeDictItem(Long id) {
		//根据ID查询字典ID
		SysDictItem dictItem = this.getById(id);
		SysDict dict = dictService.getById(dictItem.getDictId());
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystemType())) {
			return R.failed("系统内置字典项目不能删除");
		}
		boolean b = this.removeById(id);
		if(b){
			return R.ok(b,"删除成功");
		}
		return R.failed(b,"删除失败");
	}

	/**
	 * 更新字典项
	 *
	 * @param item 字典项
	 * @return
	 */
	@Override
	@CacheEvict(value = CacheConstants.DICT_DETAILS, allEntries = true)
	public R updateDictItem(SysDictItem item) {
		//查询字典
		SysDict dict = dictService.getById(item.getDictId());
		// 系统内置
		if (DictTypeEnum.SYSTEM.getType().equals(dict.getSystemType())) {
			return R.failed(null,"系统内置字典项目不能修改");
		}
		boolean b = this.updateById(item);
		if(b){
			return R.ok(b,"修改成功");
		}
		return R.failed(b,"修改失败");
	}

	@Override
	public R<IPage> getSysDictItemPage(Map map) {
		//当前页
		Long current ;
		//每页显示条数，默认 10
		Long size ;
		//获取标签名
		String label ;
		//获取字典id
		Long dictId;
		Page<SysDictItem> page = new Page<>();
		QueryWrapper queryWrapper = new QueryWrapper<SysDictItem>();
		Iterator keys = map.keySet().iterator();
		while (keys.hasNext()){
			String key = (String)keys.next();
			if("dictId".equals(key)){
				dictId  = Long.valueOf(String.valueOf(map.get("dictId")));
				if(dictId != null){
					queryWrapper.eq("dict_id",dictId);
				}
			}
			if("current".equals(key)){
				if (StrUtil.isBlank(String.valueOf(map.get("current")))){
					current = 1L;
				}else {
					current = Long.valueOf(String.valueOf(map.get("current")));
				}
				page.setCurrent(current);
			}
			if("label".equals(key)){
				label = String.valueOf(map.get("label"));
				if(StrUtil.isNotBlank(label)){
					queryWrapper.like("label",label);
				}
			}
			if("size".equals(key)){
				if (StrUtil.isBlank(String.valueOf(map.get("size")))){
					size = 10L;
				}else {
					size = Long.valueOf(String.valueOf(map.get("size")));
				}
				page.setSize(size);
			}
			queryWrapper.orderByAsc("sort");
		}

		return R.ok(this.page(page,queryWrapper));
	}

	@Override
	public SysDictItem getSysDictItem(String value, String type) {
		SysDictItem sysDictItem = this.getOne(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getValue,value).eq(SysDictItem::getType,type));
		return sysDictItem;
	}



	@Override
	public List getDictChildren(String type) {
		List<SysDictItem> dictItems = this.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getType,type));
		dictItems.forEach(sysDictItem -> {
			List dictItemsList = this.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getDictId,sysDictItem.getId()));
			sysDictItem.setChildren(dictItemsList);
		});
		return dictItems;
	}

	@Override
	public SysDictItem getDictOne(Long id, String type) {
		SysDictItem sysDictItem = this.getOne(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getValue,id).eq(SysDictItem::getType,type));
		return sysDictItem;
	}

	@Override
	public List getDictItemList(Long value, String type) {
		SysDictItem sysDictItem = this.getOne(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getValue,value).eq(SysDictItem::getType,type));
		if(StrUtil.isNotBlank(String.valueOf(sysDictItem.getId()))){
			List<SysDictItem> list = this.list(Wrappers.<SysDictItem>query().lambda().eq(SysDictItem::getDictId,sysDictItem.getId()));
			return list;
		}
		return null;
	}
}
